package com.basics.framework.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.basics.common.utils.ServletUtils;
import com.basics.framework.interceptor.annotation.RepeatSubmit;
import com.basics.framework.web.domain.RestResult;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter {
    public RepeatSubmitInterceptor() {
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            Method method = handlerMethod.getMethod();
            RepeatSubmit annotation = (RepeatSubmit)method.getAnnotation(RepeatSubmit.class);
            if (annotation != null && this.isRepeatSubmit(request)) {
                RestResult<String> ajaxResult = RestResult.error("不允许重复提交，请稍后再试");
                ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
                return false;
            } else {
                return true;
            }
        } else {
            return super.preHandle(request, response, handler);
        }
    }

    public abstract boolean isRepeatSubmit(HttpServletRequest request);
}
